<!-- 查看用户列表的页面 -->
<template>
    <div class="app-container">
        <!-- 表单 -->

            <el-form :inline="true" class="demo-form-inline">
            <el-form-item>
                <el-input v-model="userQuery.mobile" placeholder="手机号"/>
            </el-form-item>
            <el-form-item>
                <el-input v-model="userQuery.nickname" placeholder="名称"/>
            </el-form-item>
            <el-form-item label-width="80px">
                <el-select width="1px" v-model="userQuery.isDisabled" clearable placeholder="状态">
                <el-option :value="false" label="启用"/>
                <el-option :value="true" label="禁用"/>
                </el-select>
            </el-form-item>
            <el-form-item label="添加时间">
                <el-date-picker
                v-model="userQuery.begin"
                type="datetime"
                placeholder="选择开始时间"
                value-format="yyyy-MM-dd HH:mm:ss"
                default-time="00:00:00"
                />
            </el-form-item>
            <el-form-item>
                <el-date-picker
                v-model="userQuery.end"
                type="datetime"
                placeholder="选择截止时间"
                value-format="yyyy-MM-dd HH:mm:ss"
                default-time="00:00:00"
                />
            </el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="getList()">查询</el-button>
            <el-button type="default" @click="resetData()">清空</el-button>
            </el-form>

            <!-- 表格 -->
            <el-table
                v-loading="listLoading"
                :data="list"
                element-loading-text="数据加载中"
                border
                fit
                highlight-current-row>
                <el-table-column
                    label="序号"
                    width="110"
                    align="center">
                    <template slot-scope="scope">
                    {{ (page - 1) * limit + scope.$index + 1 }}
                    </template>
                </el-table-column>
                <!-- 这个就使用了前端的判断 来通过数字来反应数字 -->
                <el-table-column label="状态" width="90" align="center">
                    <template slot-scope="scope" align="center">
                    {{ scope.row.isDisabled===false?' 启用':' 禁用' }}
                    </template>
                </el-table-column>
                <el-table-column prop="nickname" label="用户" width="140" align="center" />
                <el-table-column prop="mobile" label="手机" width="190" align="center"/>
                <el-table-column label="注册方式" width="133" align="center">
                    <template slot-scope="scope" align="center">
                    {{ scope.row.openid!==null?' 微信号':'手机号' }}
                    </template>
                </el-table-column>
                <!-- <el-table-column prop="intro" label="资历" /> -->
                <el-table-column prop="gmtCreate" label="注册时间" width="190" align="center"/>
                <!-- <el-table-column prop="totalFee" label="金额" width="85" align="center"/> -->
                <el-table-column label="操作" width="300" align="center">
                    <template slot-scope="scope">
                        <!-- 进行修改 -->
                    <router-link :to="'/user/view/'+scope.row.id">
                        <el-button type="primary" size="mini" icon="el-icon-edit">查看</el-button>
                    </router-link>  
                    
                                                                <!--这个就能的到我们的这个的id  row 这一行的 然后id 就的到id-->
                    <el-button type="danger" size="mini" style="margin-left:10px" v-if="scope.row.isDisabled===false" icon="el-icon-delete" @click="disableDataById(scope.row.id)">禁用</el-button>
                    <el-button type="danger" size="mini" style="margin-left:10px" v-else icon="el-icon-delete" @click="endisableDataById(scope.row.id)">解禁</el-button>    
                    <el-button type="danger" size="mini" icon="el-icon-delete" @click="removeDataById(scope.row.id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <!-- 分页插件 -->
                <!-- 分页 -->
                <!-- page 当前页 limit 显示数  total总共-->
                <el-pagination
                :current-page="page"  
                :page-size="limit"
                :total="total"
                style="padding: 30px 0; text-align: center;"
                layout="total, prev, pager, next, jumper"
                @current-change="getList"
                />
                <!-- @current-change="getList" 分页的切换 每次切换都去查询一下 -->

    </div>
</template>
<script>
import user from '@/api/edu/user'
export default {
    data(){
        return{
            list:[], // 查询之后接口返回集合
            page:1,// 当前页
            total:0, // 总记录数
            limit:10,// 每页显示记录数
            // 订单

            listLoading: false,
            // 订单查询条件封装
            userQuery:{}
        }
    },
    created() {
        this.getList()
    },
    methods: {
        getList(page = 1){
            this.page = page
            user.getListUser(this.page,this.limit,this.userQuery).then(resp => {
                this.list = resp.data.rows
                this.total = resp.data.total
            })// 请求成功
            .catch(error => {
                console.log(error)
            }) // 请求失败
        },
        // 这个就是清空的方法
        resetData() {
            // 清空我们的输出的条件
            this.userQuery = {}
            // 再重新查询一遍
            this.getList()
        },
        // 删除用户
        removeDataById(id){
            this.$confirm('此操作将永久删除该用户，是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    return user.deleteUserById(id)
                }).then(() => {
                    this.getList(this.page)
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    })
                }).catch((response) => { // 失败
                    console.log(response)
                    if (response === 'cancel') {
                        this.$message({
                            type: 'info',
                            message: '已取消删除'
                        })
                    }
                })
        },

        // 禁用用户
        disableDataById(id){
            this.$confirm('此操作将禁用该用户，是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    return user.disableUserById(id)
                }).then(() => {
                    this.getList(this.page)
                    this.$message({
                        type: 'success',
                        message: '禁用成功!'
                    })
                }).catch((response) => { // 失败
                    console.log(response)
                    if (response === 'cancel') {
                        this.$message({
                            type: 'info',
                            message: '已取消删除'
                        })
                    }
                })
        },

        // 取消禁用
        endisableDataById(id){
            this.$confirm('此操作将解禁该用户，是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    return user.enDisableUserById(id)
                }).then(() => {
                    this.getList(this.page)
                    this.$message({
                        type: 'success',
                        message: '解禁成功!'
                    })
                }).catch((response) => { // 失败
                    console.log(response)
                    if (response === 'cancel') {
                        this.$message({
                            type: 'info',
                            message: '已取消删除'
                        })
                    }
                })
        },

        
    },
    
}
</script>